/*
 * @Description: 路由管理
 * @Author: franktrue 807615827@qq.com
 * @Date: 2021-07-28 08:18:42
 * @LastEditTime: 2021-08-02 10:24:22
 */
package routers

import (
	_ "github.com/franktrue/blog-service/docs"
	"github.com/franktrue/blog-service/internal/middleware"
	"github.com/franktrue/blog-service/internal/routers/api"
	"github.com/franktrue/blog-service/internal/routers/v1"
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

func NewRouter() *gin.Engine {
	r := gin.New()
	r.Use(gin.Logger(), gin.Recovery())
	r.Use(middleware.Translations())

	// swagger文档路由
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	// 获取Token
	r.GET("/auth", api.GetAuth)

	apiV1 := r.Group("/api/v1")

	apiV1.Use(middleware.JWT())

	tag := v1.NewTag()

	apiV1.POST("/tags", tag.Create)
	apiV1.DELETE("/tags/:id", tag.Delete)
	apiV1.PUT("/tags/:id", tag.Update)
	apiV1.PATCH("/tags/:id/state", tag.Update)
	apiV1.GET("/tags", tag.List)

	article := v1.NewArticle()

	apiV1.POST("/articles", article.Create)
	apiV1.DELETE("/articles/:id", article.Delete)
	apiV1.PUT("/articles/:id", article.Update)
	apiV1.PATCH("/articles/:id/state", article.Update)
	apiV1.GET("/articles/:id", article.Get)
	apiV1.GET("/articles", article.List)

	return r
}
